package cn.kgc.springmvcday02.controller;

import cn.kgc.springmvcday02.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 课工场
 * @date 2023/10/16
 * @description  springmvc 中参数的接收方式
 * 1. 简单类型参数的接收   int  double  String
 * 2.  包装类型
 * 3. 对象类型参数接收
 * 4. 数组形式的参数   批量删除
 * 5. json格式的数据
 */

@Controller
@RequestMapping("param")
public class ParamController {


    @RequestMapping("m1")
    public ModelAndView  method1(HttpServletRequest request){

        //参数接收
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(Integer.parseInt(age));

        System.out.println("user = " + user);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;
    }

    /**
     * 在参数列表中直接声明需要接收的参数   形参的名字需要和request对象中参数的key保持一致
     *  1.如何解决形式参数和key不一致的情况   @RequestParam
     *  2.被@RequestParam 注解修饰形参 在请求时必须要传递参数 否则抛出异常     required = true
     *
     * 使用基本类型完成参数接收时 必须要传参 否则抛出异常    int age = null
     *  1. 使用基本类型的包装类型完成参数接收    Integer age = null
     *  2. @RequestParam(defaultValue = "30" )  默认值
     *
     */

    @RequestMapping("m2")
    public ModelAndView  method2(@RequestParam(value = "uname",required = false) String username,
                                 @RequestParam(value = "pwd",required = false) String password,
                                 @RequestParam(defaultValue = "30" ) int age){

        User user = new User();
        user.setAge(age);
        user.setPassword(password);
        user.setUsername(username);

        System.out.println("user = " + user);


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;

    }


    // 参数类型可以是对象类型   对象中的属性名要和request中的key一致  ?username=tom&password=123&age=1  post{}
    @RequestMapping("m3")
    public ModelAndView  method3(User user){

        System.out.println("user = " + user);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;

    }


    // 以数组形式完成参数的接收     ids=1&ids=2&ids=3
    @RequestMapping("m4")
    public ModelAndView  method4(Integer[] ids){

        for (Integer id : ids) {
            System.out.println("id = " + id);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;

    }


    // 接收前端传递的json个数的数据  字符串  {"username":"tom","password":"123","age":20}
    // 使用@RequestBody 修时形参列表中的对象 声明前端传递的参数是json字符串 需要springmvc完成数据类型转换  json   ->  java  对象
    @RequestMapping("m5")
    public ModelAndView  method5(@RequestBody User user){

        System.out.println("user = " + user);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;

    }

}
